package com.mars.pay.controller;

import com.mars.pay.dto.WxLoginDTO;
import com.mars.pay.dto.WxPayDTO;
import com.mars.pay.service.WechatMiniAppService;
import com.mars.pay.service.WechatPayService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/pay/wechat")
@RequiredArgsConstructor
public class WechatPayController {

    private final WechatPayService wechatPayService;
    private final WechatMiniAppService wechatMiniAppService;

    /**
     * 小程序支付
     */
    @PostMapping("/mini-app")
    public Map<String, String> miniAppPay(@RequestBody WxPayDTO wxPayDTO) throws IOException {
        return wechatPayService.miniAppPay(wxPayDTO.getOrderNo(), wxPayDTO.getDescription(), wxPayDTO.getTotalFee(), wxPayDTO.getOpenid());
    }

    /**
     * 小程序支付（直接下单）
     * 前端只需要传入code，后端自动获取openid并完成支付
     */
    @PostMapping("/mini-app/direct")
    public Map<String, String> miniAppPayDirect(@RequestParam String orderNo,
                                                @RequestParam String description,
                                                @RequestParam Integer totalFee,
                                                @RequestParam String code) throws IOException {
        // 获取用户openid
        String openid = wechatMiniAppService.getOpenId(code);
        // 创建支付订单
        return wechatPayService.miniAppPay(orderNo, description, totalFee, openid);
    }

    /**
     * 小程序一键支付（自动生成订单号）
     * 前端只需要传入商品描述、支付金额和code
     */
    @PostMapping("/mini-app/quick")
    public Map<String, Object> miniAppPayQuick(@RequestParam String description,
                                               @RequestParam Integer totalFee,
                                               @RequestParam String code) throws IOException {
        // 获取用户openid
        String openid = wechatMiniAppService.getOpenId(code);

        // 生成订单号
        String orderNo = "WX" + System.currentTimeMillis() + String.format("%03d", (int) (Math.random() * 1000));

        // 创建支付订单
        Map<String, String> payParams = wechatPayService.miniAppPay(orderNo, description, totalFee, openid);

        // 封装返回结果
        Map<String, Object> result = new HashMap<>();
        result.put("orderNo", orderNo);
        result.put("payParams", payParams);

        return result;
    }

    /**
     * 扫码支付
     */
    @PostMapping("/native")
    public String nativePay(@RequestParam String orderNo,
                            @RequestParam String description,
                            @RequestParam Integer totalFee) throws IOException {
        return wechatPayService.nativePay(orderNo, description, totalFee);
    }

    /**
     * H5支付
     */
    @PostMapping("/h5")
    public String h5Pay(@RequestParam String orderNo,
                        @RequestParam String description,
                        @RequestParam Integer totalFee) throws IOException {
        return wechatPayService.h5Pay(orderNo, description, totalFee);
    }
}
